package com.ld.poetry.cloudstorage.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ld.poetry.cloudstorage.entity.Trash;
import com.ld.poetry.cloudstorage.mapper.TrashMapper;
import com.ld.poetry.config.PoetryResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 网盘文件信息表业务处理
 **/
@Service
public class TrashService {

    @Resource
    private TrashMapper trashMapper;

    /**
     * 新增
     */
    public void add(Trash trash) {
        trashMapper.insert(trash);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        trashMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            trashMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Trash trash) {
        trashMapper.updateById(trash);
    }

    /**
     * 根据ID查询
     */
    public Trash selectById(Integer id) {
        return trashMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Trash> selectAll(Trash trash) {
        return trashMapper.selectAll(trash);
    }

    /**
     * 分页查询
     */
    /**
     * 分页查询
     */
    public PoetryResult<Page<Trash>> selectPage(Trash trash, Integer pageNum, Integer pageSize) {

        // 创建 Page 对象并设置分页信息
        Page<Trash> page = new Page<>(pageNum, pageSize);

        // 调用 mapper 方法进行分页查询
        List<Trash> list = trashMapper.selectAll(trash);

        // 设置查询结果到 Page 对象
        page.setRecords(list);

        // 计算总记录数（假设有一个方法可以获取总记录数）
        long total = list.size();
        page.setTotal(total);

        // 创建 PoetryResult 对象并设置数据
        PoetryResult<Page<Trash>> result = new PoetryResult<>();
        result.setData(page);
        result.setCode(200);
        result.setMessage("查询成功");

        return result;
    }

    public void deleteByFileId(Integer fileId) {
        trashMapper.deleteByFileId(fileId);
    }
}